package com.haojiang.mh.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.haojiang.mh.R;
import com.haojiang.mh.service.entity.CardInfoBean;

import java.util.List;

/**
 * 用户卡片 -- 适配器
 * <p>
 * Created by ZapFive on 2019-05-31.
 * <p>
 * wuzhuang@mirahome.me
 */
public class ScoreUserListAdapter extends RecyclerView.Adapter<ScoreUserListAdapter.MyHolder> {

    private List<CardInfoBean> beanList;
    private Context context;
    private OnItemClickListener itemClickListener;
    private OnItemChildClickListener childClickListener;
    private int selectPosition = 0;

    public ScoreUserListAdapter(Context context, List<CardInfoBean> list) {
        this.context = context;
        this.beanList = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_score_user_list, parent, false);
        view.setOnClickListener(v -> {
            selectPosition = (Integer) v.getTag();
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, selectPosition);
            }
        });
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.itemView.setTag(holder.getAdapterPosition());
        setChildClickListener(holder.aiv_mark, holder.getAdapterPosition());
        if (beanList == null || beanList.isEmpty()) {
            holder.tv_bottom.setText(R.string.tv_empty);
            holder.tv_evaluate_score.setText(R.string.tv_empty);
            holder.tv_score.setVisibility(View.INVISIBLE);
            holder.tv_move_state.setText(R.string.tv_empty);
            holder.itemView.setBackgroundResource(R.drawable.shape_card_bg_purple_3);
        } else {
            holder.tv_score.setVisibility(View.VISIBLE);
            if (selectPosition == holder.getAdapterPosition()) {
                holder.itemView.setBackgroundResource(R.drawable.fade_purple_card);
            } else {
                holder.itemView.setBackgroundResource(R.drawable.fade_gray_card);
            }
            showCardState(holder);
            if (holder.getAdapterPosition() == 0) {
                holder.tv_bottom.setText(context.getString(R.string.txt_left_user));
            } else {
                holder.tv_bottom.setText(context.getString(R.string.txt_right_user));
            }
            holder.tv_move_state.setText(getMoveStr(beanList.get(position)));
            holder.tv_evaluate_score.setText(beanList.get(position).getScoreText());
        }
    }

    @Override
    public int getItemCount() {
        if (beanList == null || beanList.isEmpty()) {
            return 1;
        }
        return beanList.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener listener) {
        this.childClickListener = listener;
    }

    private void showCardState(MyHolder holder) {
        int type = beanList.get(holder.getAdapterPosition()).getType();
        switch (type) {
            case 0:
                holder.tv_score.setText(R.string.title_day_score);
                break;
            case 1:
                holder.tv_score.setText(R.string.title_week_score);
                break;
            case 2:
                holder.tv_score.setText(R.string.title_month_score);
                break;
        }
    }

    private void setChildClickListener(final View view, final int position) {
        view.setOnClickListener(v -> {
            if (childClickListener != null) {
                Object obj = (beanList == null || beanList.isEmpty()) ? null : beanList.get(position);
                childClickListener.onItemClick(v, obj, position);
            }
        });
    }

    private String getMoveStr(CardInfoBean bean) {
        String str;
        if (bean.getEvaluate() < 0) {
            str = context.getString(R.string.tv_empty);
        } else if (bean.getEvaluate() < 5) {
            str = context.getString(R.string.text_body_move_minimal);
        } else if (bean.getEvaluate() <= 20) {
            str = context.getString(R.string.text_body_move_occasional);
        } else if (bean.getEvaluate() <= 40) {
            str = context.getString(R.string.text_body_move_average);
        } else if (bean.getEvaluate() <= 70) {
            str = context.getString(R.string.text_body_move_above_average);
        } else {
            str = context.getString(R.string.text_body_move_frequently);
        }
        return context.getString(R.string.text_body_move_score, str);
    }

    class MyHolder extends RecyclerView.ViewHolder {

        TextView tv_bottom;
        TextView tv_move_state;
        TextView tv_evaluate_score;
        TextView tv_score;
        AppCompatImageView aiv_mark;

        MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_bottom = itemView.findViewById(R.id.tv_bottom);
            tv_move_state = itemView.findViewById(R.id.tv_move_state);
            tv_evaluate_score = itemView.findViewById(R.id.tv_evaluate_score);
            tv_score = itemView.findViewById(R.id.tv_score);
            aiv_mark = itemView.findViewById(R.id.aiv_mark);
        }
    }
}
